{template 'common/header'}
<script>
    $(function(){
        $(".to_change").on('click',function(){
            var goods_id = $(this).attr('goods_id');
            $("#change-goods-id").val(goods_id);
        });
        $(".key").keyup(function(){
            var key = $(this).val();
            var url = "{php echo $this->createWebUrl('orders',array('op' => 'change_members'))}";
            $.ajax({
                type : 'post',
                url : url,
                data : {'key_word' : key},
                success : function(data){
                    $("#member-list").html(data);
                }
            });
        });
        $(".change").on('click',function(){
            var goods_id = $("#change-goods-id").val();
            var member = $("#member-list").val();
            if(goods_id > 0 && member > 0){
                var url = "{php echo $this->createWebUrl('cashier',array('op' => 'change_member'))}";
                $.ajax({
                    type : 'post',
                    url : url,
                    data : {'order_goods_id' : goods_id,'member_id' : member},
                    success : function(data){
                        if(data == 111){
                            location.reload();
                        }else{
                            alert(data);
                        }
                    }
                });
            }
        });
        $(".day").on('change',function(){
            var day = $(this).val();
            location.href="{php echo $this->createWebUrl('cashier',array('op' => 'member_detail', 'slow_pay' => $slow_pay,'member_id' => $member_id))}&day="+day;
        });
        $(".get_money").on('click',function(){
//            var percent = "{$server['percent']}";
            var id = $(this).attr("data_id");
            var freight_price = $("#had_freight"+id).attr('price');
            var gen_price = $("#had_gen_pay"+id).attr("price");
            $("#had_freight"+id).val(freight_price);
            $("#had_gen_pay"+id).val(gen_price);
            calc();
        });
        $(".get-all-money").on('click',function(){
            var ids = {$ids};
            for(var i = 0; i < ids.length; i++){
                var id = ids[i];
                var freight_price = $("#had_freight"+id).attr('price');
                var gen_price = $("#had_gen_pay"+id).attr("price");
                $("#had_freight"+id).val(freight_price);
                $("#had_gen_pay"+id).val(gen_price);
                calc();
            }
        });
        $(".data-post").on('click',function(){
            var ids = {$ids};
            for(var i = 0; i < ids.length; i++) {
                var goods_id = ids[i];
                var had_freight = $("#had_freight"+goods_id).val();
                var had_gen = $("#had_gen_pay"+goods_id).val();
                var remark = $("#og_remark"+goods_id).val();
                $.ajax({
                    type : "post",
                    url : "{php echo $this->createWebUrl('cashier',array('op' => 'get_money'))}",
                    data : {'goods_id' : goods_id, 'had_freight' : had_freight, 'had_gen' : had_gen, 'had_remark' : remark},
                    success : function(data){
                    }
                });
            }
            location.href="{php echo $this->createWebUrl('cashier',array('op' => 'express'))}";
        });
        $(".data").on('blur',function(){
            calc();
        });
    });
    function calc(){
        var ids = {$ids};
        var all_freight = 0;
        var all_gen = 0;
        for(var i = 0; i < ids.length; i++){
            var id=ids[i];
            if($("#had_freight"+id).val() != undefined && $("#had_freight"+id).val() != '')
            {
                all_freight += parseFloat($("#had_freight" + id).val());
            }
            if($("#had_gen_pay"+id).val() != undefined && $("#had_gen_pay"+id).val() != '') {
                all_gen += parseFloat($("#had_gen_pay" + id).val());
            }
//            alert($("#had_gen_pay"+id).val());
        }
        $("#all_freight").html(all_freight);
        $("#all_gen").html(all_gen);
        var order_freight = "{$order_freight}";
        //自动计算扣除代发酬金之后的金额
        var percent = "{$server['percent']}";
//        var need_freight = ((1-parseFloat(percent))*order_freight).toFixed(2);
        var extract = (parseFloat(percent)*order_freight).toFixed(2);
        var need_freight = (all_freight - extract).toFixed(2);
        var need_get = parseFloat(need_freight) + parseFloat(all_gen);
        $("#need_get").html(need_get);
        $("#extract").html(extract);
    }
</script>
<div>
    <div class="row">
        <div class="col-xs-4"></div>
        <div class="col-xs-4">
            <input type="date" value="{$day}" class="form-control day" />
        </div>
        <div class="col-xs-4"></div>
    </div>
    <br />
    <div class="panel panel-primary">
        <div class="panel-heading">
            <div class="panel-title" style="height: 27px;">
                <div class="col-xs-4">
                    {$server['name']}
                    {if $data['o_remark']}
                    <span style="color:red;">({$data['o_remark']})</span>
                    {/if}
                    {$data['remark']}
                    提成比率：{$server['percent']}
                </div>
                <div class="col-xs-4">
                    <a href="tel:{$member['phone']}">
                        电话：{$member['phone']}
                    </a>
                </div>
                <div class="col-xs-4" align="right">
                    {$data['day']}
                </div>
            </div>
        </div>
        <div class="panel-body">
            <table class="table">
                <tr>
                    <th>订单日期</th>
                    <th>客户名</th>
                    <th>数量</th>
                    <th>单价</th>
                    <th>代付款</th>
                    <th>快递公司</th>
                    <th>单号</th>
                    <th>应收运费</th>
                    <th>实收运费</th>
                    <th>实收代付款</th>
                    <th>备注</th>
                    <th>编码</th>
                    <th>货物备注</th>
                    <th>操作</th>
                </tr>
                <tr>
                    <!--<th>商品</th>-->
                    <!--<th>数量</th>-->
                    <!--<th>单价</th>-->
                    <!--<th>运费</th>-->
                    <!--<th>代付款</th>-->
                    <!--<th>应收</th>-->
                    <!--<th>客服</th>-->
                    <!--<th>实收运费</th>-->
                    <!--<th>实收代付款</th>-->
                    <!--<th>商品备注</th>-->
                    <!--<th>收款备注</th>-->
                    <!--<th>操作</th>-->
                </tr>
                {loop $member_data $data}
                <tr>
                    <td>{$data['day']}</td>
                    <td>
                        {$data['from_member']}
                    </td>
                    <td>{$data['numbers']}</td>
                    <td>
                        {$data['freight']}
                    </td>
                    <td>
                        {$data['gen_pay']}
                    </td>
                    <td>
                        {$data['company_name']}
                    </td>
                    <td>
                        {$data['express_no']}
                    </td>
                    <td>
                        {$data['total']}
                    </td>
                    <!--<td>{$data['all_total']}</td>-->

                    <td>
                        {if $data['had_freight'] == null || $data['freight_user'] == 11}
                        <input type="text" value="{$data['had_freight']}" class="form-control data" id="had_freight{$data['id']}" name="had_freight{$data['id']}" price="{$data['total']}" />
                        {else}
                        <span style="color:green">{$data['had_freight']}</span>
                        {/if}
                    </td>
                    <td>
                        {if ($data['had_gen_pay'] == null && $data['gen_pay'] > 0) || $data['gen_user'] == 11}
                        <input type="text" value="{$data['had_gen_pay']}" class="form-control data" id="had_gen_pay{$data['id']}" name="had_gen_pay{$data['id']}" price="{$data['gen_pay']}" />
                        {else}
                        <span style="color:green">{$data['had_gen_pay']}</span>
                        {/if}
                    </td>

                    <td>
                        {if ($data['had_gen_pay'] == null && $data['gen_pay'] > 0) || $data['had_freight'] == null}
                        <input type="text" class="form-control" value="{$data['ml_remark']}" id="og_remark{$data['id']}"  />
                        {else}
                        {$data['ml_remark']}
                        {/if}

                    </td>
                    <td>
                        {$data['express_code']}
                    </td>
                    <td>{$data['og_remark']}</td>
                    <td>
                        <!--<button class="btn btn-primary to_get_money" data-toggle="modal" data-target="#myModal" goods_id="{$data['id']}">收款</button>-->
                        {if $pay_status == 1}
                        <button class="btn btn-success get_money" data_id="{$data['id']}">收齐</button>
                        {if $slow_pay == 0}
                        <button class="btn btn-warning" onclick="delay({$data['id']});">月结</button>
                        {/if}
                        <!--<button class="btn btn-danger to_change" data-toggle="modal" goods_id="{$data['id']}" data-target="#myModal2">变更收款客户</button>-->
                        {if $data['log'] == 1}
                        {/if}
                        <a href="{php echo $this->createWebUrl('cashier', array('op' => 'change_log','id' => 172))}">
                            <!--<button class="btn btn-default">变更记录</button>-->
                        </a>
                        {/if}
                    </td>
                </tr>
                {/loop}
                <tr>
                    <td colspan="2">
                    总件数：{$total_numbers}
                    </td>
                    <td colspan="6">
                    应收运费：{$freight_total}
                        |总运费：{$order_freight}

                        |应收代付款：{$gen_pay_total}
                    <!--总应收{$total}-->
                    </td>

                    <td id="all_freight"></td>
                    <td id="all_gen"></td>
                    <td colspan="3">
                        <p>提成金额：<span id="extract"></span></p>
                        <p>余应收款：<span id="need_get"></span></p>

                    </td>
                    <td>
                        {if $pay_status == 1}
                        <button class="btn btn-success get-all-money" >全部收齐</button>
                        <button class="btn btn-warning" onclick="delay_all();">全部月结</button>
                        <button class="btn btn-primary data-post">确定</button>
                        {/if}
                        <button class="btn btn-danger" onclick="javascript:window.history.go(-1);">返回</button>


                    </td>
                </tr>
            </table>
        </div>
    </div>

</div>

<!-- 变更客户 -->
<div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel2">变更收款客户</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-xs-4"></div>
                    <div class="col-xs-4">
                        <input type="text" placeholder="客户名/助记码/电话" class="form-control key" />
                    </div>
                    <div class="col-xs-4"></div>
                </div>
                <br />
                <div class="row">
                    <div class="col-xs-4"></div>
                    <div class="col-xs-4">
                        <select class="form-control" id="member-list">
                            <option>请先输入客户搜索条件</option>
                        </select>
                    </div>
                    <div class="col-xs-4"></div>
                </div>
            </div>
            <div class="modal-footer">
                <input type="hidden" value="0" id="change-goods-id" />
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary change">确定</button>
            </div>
        </div>
    </div>
</div>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">收款内容</h4>
            </div>
            <div class="modal-body">
                <input type="text" class="form-control" placeholder="收到运费" id="had_freight" />
                <br />
                <input type="text" class="form-control" placeholder="收到代付款" id="had_gen" />
                <br />
                <input type="text" class="form-control" placeholder="备注" id="had_remark" />
                <input type="hidden" value="0" id="goods-id" />
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary pay">确定</button>
            </div>
        </div>
    </div>
</div>


<script>
    $(function(){
        $(".to_get_money").click(function(){
            var goods_id = $(this).attr('goods_id');
            $("#goods-id").val(goods_id);
        });
        $(".pay").click(function(){
            var goods_id = $("#goods-id").val();
            var had_freight = $("#had_freight").val();
            var had_gen = $("#had_gen").val();
            var had_remark = $("#had_remark").val();
            var had_charge = $("#had_charge").val();
            $.ajax({
                type : "post",
                url : "{php echo $this->createWebUrl('cashier',array('op' => 'get_money'))}",
                data : {'goods_id' : goods_id, 'had_freight' : had_freight, 'had_gen' : had_gen, 'had_remark' : had_remark,'had_charge' : had_charge},
                success : function(data){
                    if(data == 111){
                        location.reload();
                    }else{
                        alert(data);
                    }
                }
            });
        });
        $(".all").click(function(){
            var goods_id = $(this).attr('goods_id');
            if(confirm("是否确定全部收齐？")){
                $.ajax({
                    type : "post",
                    url : "{php echo $this->createWebUrl('cashier',array('op' => 'get_money'))}",
                    data : {'goods_id' : goods_id, 'type' : 'all'},
                    success : function(data){
                        if(data == 111){
                            location.reload();
                        }else{
                            alert(data);
                        }
                    }
                });
            }
        });
        $(".find").change(function(){
            var region_id = $(this).val();
            location.href="{php echo $this->createWebUrl('cashier',array('op' => 'index'))}&region_id="+region_id;
        });
    });
    function delay(id){
        if(confirm("是否确定设置成月结？")){
            $.ajax({
                type : "post",
                url : "{php echo $this->createWebUrl('cashier',array('op' => 'slow'))}",
                data : {'id' : id},
                success : function(data){
                    location.reload();
                }
            });
        }
    }
    function delay_all(){
        if(confirm("是否确定全部设置成月结？")){
            var ids = {$ids};
            for(var i = 0; i < ids.length; i++){
                $.ajax({
                    type : "post",
                    url : "{php echo $this->createWebUrl('cashier',array('op' => 'slow'))}",
                    data : {'id' : ids[i]},
                    success : function(data){

                    }
                });
            }
            location.reload();
        }
    }
    function get_all(member_id){
        if(confirm("一经确定无法修改，是否确定已经收取了所有的款项？")){
            $.ajax({
                type : 'post',
                url : "{php echo $this->createWebUrl('cashier',array('op' => 'get_all'))}",
                data : {'member_id' : member_id},
                success : function(data){
                    if(data == 111){
                        location.reload();
                    }else{
                        alert(data);
                    }
                }
            });
        }
    }
</script>
{template 'common/footer'}